package httpHandler

import (
	baseHttp "gitee.com/zaiqiang231/go-base-app/base_app/http"
	"gitee.com/zaiqiang231/go-base-session/app/model"
	"gitee.com/zaiqiang231/go-base-session/app/util"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
)

func CreateSession(context *gin.Context) {
	var request model.SessionCreateRequest
	if err := context.ShouldBindBodyWith(&request, binding.JSON); err != nil {
		baseHttp.WriteResponse(context, http.StatusBadRequest, err.Error(), nil)
		return
	}
	sid, err := util.CreateSession(context, request.Uid, request.ExpAt, request.CV)
	if err != nil {
		baseHttp.WriteResponse(context, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(context, 0, "success", gin.H{
		"sid": sid,
	})
}

func CheckSession(context *gin.Context) {
	var request model.SessionCheckRequest
	if err := context.ShouldBindBodyWith(&request, binding.JSON); err != nil {
		baseHttp.WriteResponse(context, http.StatusBadRequest, err.Error(), nil)
		return
	}
	isValid, err := util.CheckSession(context, request.Uid, request.Sid)
	if err != nil {
		baseHttp.WriteResponse(context, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(context, 0, "success", gin.H{
		"isValid": isValid,
	})
}
